package habitcoach.core.habit;

import habitcoach.core.Environmental;
import habitcoach.core.SystemEnvironment;
import util.Constants;

/**
 * Factory that creates @Habit
 * 
 * @author Coddy Created at 2010-1-28 上午10:55:12
 */
public class HabitFactory
{
	private volatile static HabitFactory instance;
	private static Environmental env;
	private static int habitCount = 0;

	// private HabitFactory()
	// {
	// }

	private HabitFactory(Environmental environmental)
	{
		env = environmental;
	}

	public static HabitFactory getInstance()
	{
		return getInstance(new SystemEnvironment());
	}

	/**
	 * Create a HabitFactory using environment as variable. NOTE the env is
	 * static and will STAY THE SAME unless set to another precisely.
	 * 
	 * @param environment
	 * @return
	 */
	public static HabitFactory getInstance(Environmental environment)
	{
		if (instance == null)
		{
			synchronized (HabitFactory.class)
			{
				if (instance == null)
				{
					instance = new HabitFactory(environment);
				}
			}
		} else
		{
			env = environment;
		}

		return instance;
	}

	/**
	 * Create Habit using a question
	 * 
	 * @param question
	 * @return
	 */
	public Habit createHabit(String question)
	{
		HabitImpl habit = new HabitImpl(env, Constants.DEFAULT_TITLE,
				Constants.DEFAULT_CATEGORY, question);
		return habit;
	}

	/**
	 * Create Habit using title, category and question
	 * 
	 * @param question
	 * @return
	 */
	public Habit createHabit(String title, String category, String question)
	{
		HabitImpl habit = new HabitImpl(env, title, category, question);
		habitCount++;
		habit.setID(habitCount);
		return habit;
	}

	/**
	 * Create Habit using default values.
	 * 
	 * @return
	 */
	public Habit createHabit()
	{
		Habit habit = createHabit(Constants.DEFAULT_TITLE,
				Constants.DEFAULT_CATEGORY, Constants.DEFAULT_DAILY_QUESTION);
		return habit;
	}

	public static Environmental getEnv()
	{
		return env;
	}

	public static void setEnv(Environmental env)
	{
		HabitFactory.env = env;
	}
}
